/*
 * This file is part of JContextFree.
 *
 * JContextFree is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * JContextFree is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with JContextFree.  If not, see <http://www.gnu.org/licenses/>.
 */

package it.enricod.jcontextfree.engine.model.impl;

import java.util.ArrayList;
import java.util.List;

import it.enricod.jcontextfree.engine.model.FigureRegion;
import it.enricod.jcontextfree.engine.model.IFigure;
import it.enricod.jcontextfree.engine.model.IPaint;
import it.enricod.jcontextfree.engine.model.vo.Position2D;

/**
 * classe che descrive il disegno completo
 * 
 * @author enrico
 *
 */
public class Paint implements IPaint
{

    /**
     * punto in basso a sinistra
     */
    private Position2D p1 = Position2D.createInstance(0d, 0d);
    /**
     * punto in alto a destra
     */
    private Position2D p2 = p1;
    /**
     * elenco delle figure che compongono il disegno
     */
    private List<IFigure> figures = new ArrayList<IFigure>();

    public int getPaintWidth()
    {
        return new Double(p2.getX() - p1.getX() ).intValue();
    }

    public int getPaintHeight()
    {
        return new Double (p2.getY() - p1.getY() ).intValue();
    }

    public List<IFigure> getFigures()
    {
        return figures;
    }

    public void addFigure(IFigure figure)
    {
        FigureRegion region = figure.getFigureRegion();

        double x1 = Math.min(p1.getX(), region.getP1().getX());
        double y1 = Math.min(p1.getY(), region.getP1().getY());

        double x2 = Math.max(p2.getX(), region.getP2().getX());
        double y2 = Math.max(p2.getY(), region.getP2().getY());

        p1 = Position2D.createInstance(x1, y1);
        p2 = Position2D.createInstance(x2, y2);

        figures.add(figure);
    }

    public FigureRegion getRegion()
    {
        return new FigureRegion(p1, p2);
    }

    /**
     * ricostruiamo un nuovo disegno spostando le singole figure
     */
    public IPaint translate(double dx, double dy)
    {
        Paint result = new Paint();
        for (IFigure figure : getFigures())
        {
            IFigure newFigure = figure.translate(dx, dy);
            result.addFigure(newFigure);
        }
        return result;
    }

    public IPaint translateInFirstQuadrant()
    {
        FigureRegion region = getRegion();
        return translate(-region.getP1().getX(), -region.getP1().getY());
    }

    public IPaint scale(double factor)
    {
        Paint result = new Paint();
        for (IFigure figure : getFigures())
        {
            IFigure newFigure = figure.scale(factor);
            result.addFigure(newFigure);
        }
        return result;
    }
}
